package t20220304;

import java.util.Arrays;

/**
 * @Author: DengJia
 * @Date: 2022/3/4 21:58
 * @Description: String上的操操作
 */

public class StringOperation {
    public static void main(String[] args) {
        // 1、创建String对象
        String s = new String("qwertyuiopasdfghjklzxcvbnm");
        System.out.println("s = " + s);

        // 2、String中的字符个数
        System.out.println("s.length() = " + s.length());

        // 3、取索引字符
        System.out.println("s.charAt(17) = " + s.charAt(17));

        // 4、复制到目标数组
        char[] cs = new char[17];
        s.getChars(0, 10, cs, 7);
        System.out.println("cs = " + Arrays.toString(cs));
        byte[] bytes = s.getBytes();
        System.out.println("bytes = " + Arrays.toString(bytes));

        // 5、生成字符数组
        System.out.println("s.toCharArray() = " + Arrays.toString(s.toCharArray()));

        // 6、equals
        System.out.println("\"qq\".equals(\"QQ\") = " + "qq".equals("QQ"));
        System.out.println("\"pp\".equalsIgnoreCase(\"PP\") = " + "pp".equalsIgnoreCase("PP"));

        // 7、比较
        System.out.println("\"a\".compareTo(\"z\") = " + "a".compareTo("z"));
        System.out.println("\"z\".compareTo(\"z\") = " + "z".compareTo("z"));
        System.out.println("\"z\".compareTo(\"a\") = " + "z".compareTo("a"));
        System.out.println("\"a\".compareTo(\"A\") = " + "a".compareTo("A"));

        // 8、包含
        System.out.println("s.contains(\"a\") = " + s.contains("a"));
        System.out.println("s.contains(\"A\") = " + s.contains("A"));

        // 9、内容完全匹配
        System.out.println("\"aBC\".contentEquals(new StringBuilder(\"aBC\")) = " + "aBC".contentEquals(new StringBuilder("aBC")));

        // 10、忽略大消息比较
        System.out.println("\"ABC\".equalsIgnoreCase(\"abc\") = " + "ABC".equalsIgnoreCase("abc"));

        // 11、区域匹配
        boolean regionMatches = s.regionMatches(true, 2, "ERTYU", 0, 5);
        System.out.println("regionMatches = " + regionMatches);

        // 12、以什么开始
        System.out.println("s.startsWith(\"qwe\") = " + s.startsWith("qwe"));
        System.out.println("s.startsWith(\"ert\", 2) = " + s.startsWith("ert", 2));

        // 13、以什么结束
        System.out.println("s.endsWith(\"bnm\") = " + s.endsWith("bnm"));

        // 14、参数的索引位置
        System.out.println("s.indexOf(\"k\") = " + s.indexOf("k"));
        System.out.println("s.lastIndexOf(\"k\") = " + s.lastIndexOf("k"));

        // 15、子串 子序列
        System.out.println("s.substring(17) = " + s.substring(17));
        System.out.println("s.substring(17, 20) = " + s.substring(17, 20));
        System.out.println("s.subSequence(17, 20) = " + s.subSequence(17, 20));

        // 16、连接String
        String concat = "中".concat("国").concat("万").concat("岁");
        System.out.println("concat = " + concat);

        // 17、替换（有替换则返回替换后的字符串，否则返回原来的字符串）
        String r1 = s.replace("A", "AAA");
        String r2 = s.replace("a", "aaa");
        System.out.println("r1 = " + r1);
        System.out.println("r2 = " + r2);

        // 18、大小写转换
        System.out.println("s.toUpperCase() = " + s.toUpperCase());
        System.out.println("\"AAA\".toLowerCase() = " + "AAA".toLowerCase());

        // 19、字符串修剪（去掉前后空白字符）
        System.out.println("\"    origin    \" = " + "    origin    ");
        System.out.println("\"    origin    \".trim() = " + "    origin    ".trim());

        // 20、返回参数的String
        Object o = 1;
        char[] ca = {'a', 'b', 'c', 'd', 'e'};
        boolean b = true;
        char c = 'k';
        int i = 10;
        long l = 100;
        float f = 1.1F;
        double d = 1.11;

        System.out.println("String.valueOf(o) = " + String.valueOf(o));
        System.out.println("String.valueOf(ca) = " + String.valueOf(ca));
        System.out.println("String.valueOf(ca, 1, 3) = " + String.valueOf(ca, 1, 3));
        System.out.println("String.valueOf(c) = " + String.valueOf(c));
        System.out.println("String.valueOf(i) = " + String.valueOf(i));
        System.out.println("String.valueOf(l) = " + String.valueOf(l));
        System.out.println("String.valueOf(f) = " + String.valueOf(f));
        System.out.println("String.valueOf(d) = " + String.valueOf(d));

        // 21、为每个唯一的字符序列生成一个且仅生成一个String引用
        String intern = s.intern();
        System.out.println("intern = " + intern);

        // 22、将字符串从正则表达式匹配的地方切开（可以限制分割的次数）
        String str = "1'2.3*4+5-6";
        String[] split = str.split("['.*\\-+]", 4);
        System.out.println("split = " + Arrays.toString(split));
    }
}
